<?php
namespace Gotcha\Core {
    final class AppAutoloader {
        const THROW_EXCEPTION = true;
        const PREPEND_AUTOLOADER = true;

        public function __construct($autoloadExtensions) {
            spl_autoload_extensions($autoloadExtensions);
            spl_autoload_register(function($className) {
                // Check $className - if it is internal class
                // classified in Gotcha namespace, remove global
                // part for a path to a class
                if(preg_match('/^Gotcha/', $className)) {
                    $classNameElements = new \ArrayObject(explode('\\', $className));
                    $classNameElements->offsetUnset(0);
                    $className = implode('\\', $classNameElements->getArrayCopy());
                }

                /** @noinspection PhpIncludeInspection */
                require_once $className . '.php';
            }, self::THROW_EXCEPTION, self::PREPEND_AUTOLOADER);
        }
    }
}

